package com.itnoob.strategy.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.itnoob.strategy.service.PayService;
import com.itnoob.strategy.service.PayStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author zhoujun
 * @date 2024/12/5 22:19
 */
@RestController
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private  Map<String,PayStrategy> payStrategyMap;

    /**
     * localhost:8080/strategy/pay?amount=100&type=wechat
     * @param amount
     * @param type
     * @return
     */
    @RequestMapping("/pay")
    public String pay(@RequestParam("amount") double amount, @RequestParam("type") String type) {

        //校验
        //第一种方式使用策略模式
        PayService payService = (PayService) SpringUtil.getBean(PayService.class);
        PayStrategy payStrategy = (PayStrategy) SpringUtil.getBean(type);
        payService.setStrategy(payStrategy);
        String result = payService.pay(BigDecimal.valueOf(amount));
        return result;
    }


    @RequestMapping("/pay2")
    public String pay2(@RequestParam("amount") double amount, @RequestParam("type") String type) {

        //校验
        //第二种方式使用策略模式
        PayService payService = (PayService) SpringUtil.getBean(PayService.class);
        PayStrategy payStrategy = payStrategyMap.get(type);
        payService.setStrategy(payStrategy);
        String result = payService.pay(BigDecimal.valueOf(amount));
        return result;
    }
}
